package com.javaee.xiangyang.controller.mall;

import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.dto.ShopCarGoods;
import com.javaee.xiangyang.entity.ShopCar;
import com.javaee.xiangyang.entity.User;
import com.javaee.xiangyang.service.ShopCarService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/MallCar")
public class MallCarController {

    @Resource
    ShopCarService shopCarService;

    /**
     * 获取当前用户的购物车信息
     */
    @RequestMapping("/MyCar")
    public ModelAndView myCar(ModelAndView modelAndView, HttpSession session) {
        //获取当前session中用户的账号信息
        User user = (User) session.getAttribute("user");
        //封装入ShopCar内
        ShopCar shopCar = new ShopCar();
        shopCar.setShopUserAccount(user.getUserAccount());
        List<ShopCarGoods> carByUserAccount = shopCarService.findCarByUserAccount(shopCar);
        //计算当前购物车总价
        BigDecimal Total = BigDecimal.ZERO;
        for (ShopCarGoods shopCarGoods : carByUserAccount) {
            Total = Total.add(shopCarGoods.getShopCar().getShopTotal());
        }
        modelAndView.addObject("shopCar", carByUserAccount);
        modelAndView.addObject("Total", Total);
        modelAndView.setViewName("/mall/basket");
        return modelAndView;
    }

    /**
     * 用户添加商品到购物车
     * 为什么要用session  因为我用th表达式传递${session.user.userAccount}会报错！
     */
    @RequestMapping("/AddCar")
    public ModelAndView addCar(ModelAndView modelAndView, ShopCar shopCar, HttpSession session) {
        //获取用户登录ID
        User user = (User) session.getAttribute("user");
        String userAccount = user.getUserAccount();
        //封装
        shopCar.setShopUserAccount(userAccount);
        ServiceResultEnum serviceResultEnum = shopCarService.insertShopCar(shopCar);
        if (serviceResultEnum == ServiceResultEnum.SUCCESS) {
            //填入成功
            modelAndView.setViewName("redirect:/MallGoods/index");
            return modelAndView;
        }
        //失败
        modelAndView.addObject("error", serviceResultEnum.getResult());
        modelAndView.setViewName("/error/error");
        return modelAndView;
    }

    /**
     * 删除当前购物车信息
     */
    @RequestMapping("/DeleteCarOfOne")
    public ModelAndView deleteCarOfOne(ModelAndView modelAndView, ShopCar shopCar, HttpSession session) {
        //获取session中的用户信息
        User user = (User) session.getAttribute("user");
        //封装入当前封装类
        shopCar.setShopUserAccount(user.getUserAccount());
        ServiceResultEnum serviceResultEnum = shopCarService.deleteCarByUserAccountAndGoodsId(shopCar);
        if (serviceResultEnum == ServiceResultEnum.SUCCESS) {
            //删除成功
            modelAndView.setViewName("redirect:/MallCar/MyCar");
            return modelAndView;
        }
        //删除失败
        modelAndView.addObject("error", serviceResultEnum.getResult());
        modelAndView.setViewName("/error/error");
        return modelAndView;
    }

    /**
     * 删除整个购物车
     */
    @RequestMapping("/DeleteCarAll")
    public ModelAndView deleteCarAll(ModelAndView modelAndView, HttpSession session) {
        User user = (User) session.getAttribute("user");
        ServiceResultEnum serviceResultEnum = shopCarService.deleteCarByUserAccount(new ShopCar(user.getUserAccount()));
        if (serviceResultEnum == ServiceResultEnum.SUCCESS) {
            //删除成功
            modelAndView.setViewName("redirect:/MallCar/MyCar");
            return modelAndView;
        }
        //删除失败
        modelAndView.setViewName("/error/error");
        return modelAndView;

    }

    /**
     * 更改用户购物车购物信息
     */
    @RequestMapping("/UpdateCar")
    public ModelAndView updateCar(ModelAndView modelAndView, HttpSession session, ShopCar shopCar) {
        User user = (User) session.getAttribute("user");
        shopCar.setShopUserAccount(user.getUserAccount());
        ShopCarGoods carById = shopCarService.findCarById(shopCar);
        if (carById != null && carById.getShopCar().getShopUserAccount().equals(shopCar.getShopUserAccount())) {
            //购物车存在  且存在于此用户的购物车内
            if (shopCar.getShopNumber() == 0) {
                //此处直接删除该条购物车信息
                shopCarService.deleteCarByUserAccountAndGoodsId(shopCar);
                modelAndView.setViewName("redirect:/MallCar/MyCar");
                return modelAndView;
            } else if (shopCar.getShopNumber() < 0) {
                //此处是非法请求了小于0是非法的
                modelAndView.addObject("error", ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult());
                modelAndView.setViewName("redirect:/MallCar/MyCar");
                return modelAndView;
            } else {
                shopCar.setShopGoodsId(carById.getGoods().getGoodsId());
                shopCarService.updateCarNumberBy(shopCar);
                modelAndView.setViewName("redirect:/MallCar/MyCar");
                return modelAndView;
            }
        }
        modelAndView.addObject("error", ServiceResultEnum.SHOPPING_CART_NOT_EXIST.getResult());
        modelAndView.setViewName("/error/error");
        return modelAndView;
    }


}
